package org.wenhuayuan.library.resful

import java.lang.reflect.Method
import java.lang.reflect.Proxy
import java.util.concurrent.ConcurrentHashMap

open class HiResful constructor(val baseUrl: String, val callFactory: HiCall.Factory) {
    private var interceptors: MutableList<HiInterceptor> = mutableListOf()
    private var methodService: ConcurrentHashMap<Method, MethodParser> = ConcurrentHashMap()
    private var scheduler: Scheduler

    init {
        scheduler = Scheduler(callFactory, interceptors)
    }
    fun addInterceptor(interceptor: HiInterceptor) {
        interceptors.add(interceptor)
    }

    fun <T> create(service: Class<T>): T {
        return Proxy.newProxyInstance(
            service.classLoader,
            arrayOf<Class<*>>(service)
        ) { proxy, method, args ->
            //bugFix:此处需要考虑 空参数
            var methodParser = methodService.get(method)
            if (methodParser == null) {
                methodParser = MethodParser.parse(baseUrl, method)
                methodService[method] = methodParser
            }

            //bugFix：此处 应当考虑到 methodParser复用，每次调用都应当解析入参
            val request = methodParser.newRequest(method, args)
            scheduler.newCall(request)
        } as T
    }
//    fun <T> create(service: Class<T>): T {
//        return Proxy.newProxyInstance(
//            service.classLoader,
//            arrayOf<Class<*>>(service),
//            { _, method, args ->
//                if (methodService.get(method) == null) {
//                    methodParser = MethodParser.parse(baseUrl, method, args)
//                    methodService.put(method, methodParser!!)
//                }
//                val request = methodParser?.newRequest()
////                callFactory.newCall(request!!)
//                scheduler.newCall(request!!)
//            }) as T
//    }
}